<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <script src="./js/jquery-3.5.1.min.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body{
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
        }
        .form-wrapper{
            padding: 20px;
            border: 1px solid #ddd;
            min-width: 400px;
        }
        .form-wrapper .row{
            margin: 10px 0;
        }
        .form-wrapper .row>label{
            display: inline-block;
            min-width: 4em;
        }
        .error{
            color: red;
        }
    </style>
</head>

<body>
    <div class="form-wrapper">
        <h1>注册</h1>
        <form id="signUpForm"> 
            <div class="row">
                <label>邮箱</label>
                <input type="text" name="email">
                <span class="error"></span>
            </div>
            <div class="row">
                <label>密码</label>
                <input type="password" name="password">
                <span class="error"></span>
            </div>
            <div class="row">
                <label>确认密码</label>
                <input type="password" name="password_confirmation">
                <span class="error"></span>
            </div>    
            <div class="row">
                <input type="submit" value="注册">
            </div>      
        </form>
   </div>
    <script>
        let $form = $('#signUpForm');
       let hash = {};
       $form.on('submit',(e)=>{
           e.preventDefault();
           let need = ['email', 'password', 'password_confirmation'];
           need.forEach((name)=>{
               let val = $form.find(`input[name=${name}]`).val();
               hash[name]=val;
           })
           //每次点击提交后先清空.error里的文字
           $('.error').text('')
           if(hash.email === ''){
               $form.find('[name=email]').siblings('.error').text('请填写邮箱')
               return
           }
           if(hash.password === ''){
               $form.find('[name=password]').siblings('.error').text('请填写密码')
               return
           }
           if(hash.password_confirmation === ''){
               $form.find('[name=password_confirmation]').siblings('.error').text('请确认密码')
               return
           }
           if(hash.password_confirmation !== hash.password){
               $form.find('[name=password_confirmation]').siblings('.error').text('密码不匹配')
               return
           }
           $.post('/reg', hash)
            .then((response)=>{
                // console.log(response);
                console.log(hash);
            },(request)=>{
                let {errors} = request.responseJSON
                if(errors.email && errors.email === 'invalid'){
                    $form.find('[name=email]').siblings('.error').text('你的邮箱不正确')
                }
            })
       })
    </script>
</body>

</html>